package com.dyh.quickdemozwei.mdh;

import com.alibaba.fastjson.JSONObject;

public interface FieldWriters {

    interface FieldWriterJson extends FieldWriter<JSONObject> {}

    FieldWriterJson json = (body, val ,clue, extendArgs) ->{
        if (body==null)
            body = new JSONObject();

        if (clue.path()==null||clue.path().length()<=0)
             body.put(clue.key(),val);

        PathAnalyzer pathAnalyzer =
                SmartArgUtils.elect(SmartArgUtils.softArgGet(0,extendArgs,PathAnalyzer.class),
                        PathAnalyzers.defaultPathAnalyzer);

        String[] pathArry = pathAnalyzer.work(clue.path());

        JSONObject node =body;
        for (String path : pathArry) {
            Object raw =  node.get(path);
            if (raw instanceof JSONObject){
                node = (JSONObject)raw;
            }
            else{
                JSONObject neoJo = new JSONObject();
                node.put(path,neoJo);
                node =neoJo;

            }
        }
         node.put(clue.key(),val);
    };
}
